package com.ldzy.webase.dao;

import com.ldzy.webase.entity.Sports;
import com.ldzy.webase.entity.SportsOrder;
import com.ldzy.webase.entity.SportsTime;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.sql.Date;
import java.util.List;

/**
 * @author ：Lq
 * @version ：1.0
 * @date ：2022/11/15 17:16
 * @description ：t_sports-场馆预约表-数据层
 */
public interface SportsMapper {
    // 查询所有场馆信息，可根据场馆类型查询
    @Select("<script>" +
            "select * from t_sports " +
            "<where> " +
            "<if test=\"type != null and type != ''\"> " +
            "type=#{type}" +
            "</if>" +
            "</where>" +
            "</script>")
    List<Sports> getAllSports(@Param("type") String type);

    // 根据场馆id获取场馆详情信息
    @Select("select * from t_sports where sportsId=#{sportsId}")
    Sports getSportsBySportsId(@Param("sportsId") Integer sportsId);

    // 根据场馆id获取场馆的 预约时间-开始 至 预约时间-结束
    // 可根据预约日期查询是否已被预定（可选）
    @Select("<script>" +
            "select spt.*,spo.uid,spo.orderDate " +
            "from t_sports_time spt " +
            "left join t_sportsorder spo on spo.orderStartDate = spt.startDate and " +
            "spo.orderEndDate = spt.endDate " +
            "<if test=\"orderDate != null\"> " +
            "and spo.orderDate=#{orderDate} " +
            "</if>" +
            "and spo.sportsId = spt.sportsId " +
            "<where> " +
            "spt.sportsId=#{sportsId} " +
            "</where>" +
            "order by " +
            "spt.startDate asc" +
            "</script>")
    List<SportsTime> getSportsStartEndDateBySportsId(@Param("sportsId") Integer sportsId, @Param("orderDate") Date orderDate);

    // 根据学生uid和场馆sportsId添加预约记录信息
    @Insert("insert into t_sportsorder(sportsId,uid,orderDate,orderStartDate,orderEndDate) " +
            "values(#{sportsOrder.sportsId},#{sportsOrder.uid},#{sportsOrder.orderDate},#{sportsOrder.orderStartDate},#{sportsOrder.orderEndDate})")
    int addSportsOrder(@Param("sportsOrder") SportsOrder sportsOrder);

    // 根据学生uid查询已预约的记录信息
    @Select("select spo.*,sp.name,sp.type,sp.phoneNumber,sp.money,sp.address " +
            "from t_sportsorder as spo " +
            "left join t_sports sp on sp.sportsId = spo.sportsId " +
            "where spo.uid=#{uid}")
    List<SportsOrder> getSportsOrderByUid(@Param("uid") Integer uid);
}